package array;

/**
 * @ Author     ：侯耀祖
 * @ Description：
 */
public class minSubArrayLen {
    public int minSubArrayLen(int target, int[] nums) {
        int sum = 0, j=0, len = Integer.MAX_VALUE;
        for (int i = 0; i<nums.length-1; i++){
            sum += nums[i];
            while (sum >=target){
                len = Math.min(len, i - j + 1);
                sum -= nums[j++];
            }
        }
        return len==Integer.MAX_VALUE?0:len;
    }
    public int minSubArrayLen2(int target, int[] nums) {
        int sum = 0, len = Integer.MAX_VALUE;
        for(int i=0; i<nums.length; i++){
            sum = 0;
            for(int j=i; j<nums.length; j++){
                sum += nums[j];
                if(sum>=target){
                    len = Math.min(len, j - i + 1);
                    break;
                }
            }
        }
        return len==Integer.MAX_VALUE?0:len;
    }
}
